/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using Xceed.Silverlight.Data.Stats;

namespace Xceed.Silverlight.Data
{
  internal abstract class AsyncParentEnumerator<T> : AsyncEnumerator<T>
  {
    protected AsyncParentEnumerator()
    {
    }

    protected AsyncParentEnumerator( IAsyncEnumerator childEnumerator )
    {
      this.ChildEnumerator = childEnumerator;
    }

    public override bool IsFullySynchronized
    {
      get
      {
        return m_childEnumerator.IsFullySynchronized;
      }
    }

    protected internal IAsyncEnumerator ChildEnumerator
    {
      get
      {
        return m_childEnumerator;
      }
      set
      {
        if( m_childEnumerator != null )
          throw new InvalidOperationException( "An attempt was made to set a ChildEnumerator when one has already been provided." );

        if( m_childEnumerator != value )
        {
          if( m_childEnumerator != null )
          {
            m_childEnumerator.DataChanged -= ChildDataChanged;
          }

          m_childEnumerator = value;

          if( m_childEnumerator != null )
          {
            m_childEnumerator.DataChanged += ChildDataChanged;
          }
        }
      }
    }

    protected internal IAsyncEnumerator<T> ChildEnumeratorTyped
    {
      get
      {
        return ( IAsyncEnumerator<T> )m_childEnumerator;
      }
    }

    public override IAsyncResult BeginGroupCount( CountParameters parameters, AsyncCallback userCallback, object asyncState )
    {
      return m_childEnumerator.BeginGroupCount( parameters, userCallback, asyncState );
    }

    public override GroupCountResult EndGroupCount( IAsyncResult asyncResult )
    {
      return m_childEnumerator.EndGroupCount( asyncResult );
    }

    public override IAsyncResult BeginCount( CountParameters parameters, AsyncCallback userCallback, object asyncState )
    {
      return m_childEnumerator.BeginCount( parameters, userCallback, asyncState );
    }

    public override CountResult EndCount( IAsyncResult asyncResult )
    {
      return m_childEnumerator.EndCount( asyncResult );
    }

    public override IAsyncResult BeginGetStatValues( StatParameters parameters, AsyncCallback userCallback, object asyncState )
    {
      return m_childEnumerator.BeginGetStatValues( parameters, userCallback, asyncState );
    }

    public override StatResults EndGetStatValues( IAsyncResult asyncResult )
    {
      return m_childEnumerator.EndGetStatValues( asyncResult );
    }

    public override JumpMoveAndFetchAsyncResult BeginJumpMoveAndFetch( 
      JumpMoveAndFetchParameters parameters, 
      AsyncCallback userCallback, 
      object asyncState )
    {
      return m_childEnumerator.BeginJumpMoveAndFetch( 
               parameters, 
               userCallback, 
               asyncState );
    }

    public override JumpMoveAndFetchResult<T> EndJumpMoveAndFetch( 
      JumpMoveAndFetchAsyncResult jumpMoveAndFetchAsyncResult )
    {
      return ( ( IAsyncEnumerator<T> )m_childEnumerator ).EndJumpMoveAndFetch( jumpMoveAndFetchAsyncResult );
    }

    private void ChildDataChanged( object sender, DataChangedEventArgs args )
    {
      this.ChildDataChanged( args );
    }

    protected virtual void ChildDataChanged( DataChangedEventArgs args )
    {
      this.RaiseDataChanged( args );
    }

    #region IDisposable code
    private bool m_disposed;//false
    protected override void Dispose( bool disposing )
    {
      if( !m_disposed )
      {
        try
        {
          if( disposing && m_childEnumerator != null)
          {
            m_childEnumerator.Dispose();
          }
          m_disposed = true;
        }
        finally
        {
          base.Dispose(disposing);
        }
      }
    }
    #endregion


    #region Private Fields

    private IAsyncEnumerator m_childEnumerator; //null

    #endregion Private Fields
  }
}
